package cn.swust.designpatternlearningplatform.service.impl;

import cn.swust.designpatternlearningplatform.dao.*;
import cn.swust.designpatternlearningplatform.domain.bo.AnswerBo;
import cn.swust.designpatternlearningplatform.domain.bo.TestPaperDataBo;
import cn.swust.designpatternlearningplatform.domain.dto.StudentPaperListDto;
import cn.swust.designpatternlearningplatform.domain.entity.DailyExercise;
import cn.swust.designpatternlearningplatform.domain.entity.PaperResult;
import cn.swust.designpatternlearningplatform.domain.entity.TestPaper;
import cn.swust.designpatternlearningplatform.domain.vo.StudentSubmitTestDataVo;
import cn.swust.designpatternlearningplatform.service.StudentTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional
public class StudentTestServiceImpl implements StudentTestService {
    @Resource
    private TopicDao topicDao;
    @Resource
    private StudentTestDao studentTestDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private PaperResultDao paperResultDao;
    @Resource
    private ObtainResultsDao obtainResultsDao;

    /**
     * @param studentSubmitTestDataVo:
     * @param studentNumber:
     * @return void
     * @author wang
     * description 提交学生的答案
     * date 2023/5/9 20:17
     */
    @Override
    public void postAnswer(StudentSubmitTestDataVo studentSubmitTestDataVo, String studentNumber) {
        String type = studentSubmitTestDataVo.getType();
        String paperNumber = studentSubmitTestDataVo.getPaperNumber();
        List<AnswerBo> answerBo = studentSubmitTestDataVo.getAnswer();
        LocalDateTime time = LocalDateTime.now();
        int sum = 0;
        for (int i = 0; i < answerBo.size(); i++) {
            PaperResult paperResult1 = new PaperResult();
            DailyExercise dailyExercise = new DailyExercise();
            String answer = topicDao.getAnswer(answerBo.get(i).getTopicId());
            paperResult1.setPaperNumber(paperNumber);
            paperResult1.setStudentNumber(studentNumber);
            paperResult1.setTopicId(answerBo.get(i).getTopicId());
            paperResult1.setOwnAnswer(answerBo.get(i).getOwnAnswer());
            dailyExercise.setStudentNumber(studentNumber);
            dailyExercise.setOwnAnswer(answerBo.get(i).getOwnAnswer());
            dailyExercise.setTopicId(answerBo.get(i).getTopicId());
            dailyExercise.setProblemTime(time);
            if (Objects.equals(answer, answerBo.get(i).getOwnAnswer())) {
                paperResult1.setIsRight(1);
                dailyExercise.setIsRight(1);
                if (Objects.equals(type, "testPaper")) {
                    sum = sum + obtainResultsDao.getScore(paperNumber, answerBo.get(i).getTopicId());
                }
            } else {
                //错误情况
                paperResult1.setIsRight(0);
                dailyExercise.setIsRight(0);
            }
            paperResultDao.insertPaperResult(paperResult1);
            studentTestDao.insertDailyExercise(dailyExercise);
        }

        if (Objects.equals(type, "testPaper")) {
            studentTestDao.insertScore(paperNumber, studentNumber, sum);
        }
    }

    /**
     * @param studentNumber:
     * @return List<StudentPaperListDto>
     * @author wang
     * description 返回前端需要的试卷的列表
     * date 2023/5/14 15:08
     */
    @Override
    public List<StudentPaperListDto> getTestList(String studentNumber) {

        //获取与学生相关的一些绑定消息
        TestPaperDataBo testPaperDataBo = studentDao.getStudentTestPaperData(studentNumber);
        List<StudentPaperListDto> list = new ArrayList<>();
        List<TestPaper> testPapers = studentTestDao.getTestList(testPaperDataBo);
        for (TestPaper testPaper : testPapers) {
            StudentPaperListDto studentPaperListDto = new StudentPaperListDto();
            studentPaperListDto.setId(testPaper.getPaperNumber());
            studentPaperListDto.setStartTime(String.valueOf(testPaper.getStartTime()));
            studentPaperListDto.setTestTime(testPaper.getDoableTime());
            studentPaperListDto.setTitle(testPaper.getTestName());
            studentPaperListDto.setEndTime(String.valueOf(testPaper.getEndTime()));
            studentPaperListDto.setProblemNum(testPaper.getTopicNumber());
            studentPaperListDto.setPublisher(teacherDao.getTeacherByNumber(testPaper.getTeacherNumber()).getTeacherName());
            studentPaperListDto.setPublishTime(String.valueOf(testPaper.getPublishTime()));
            List<PaperResult> paperResult = paperResultDao.getPaperResult(studentNumber, testPaper.getPaperNumber());
            if (!paperResult.isEmpty()) {
                studentPaperListDto.setIsSubmit(true);
                int sum = 0;
                for (PaperResult result : paperResult) {
                    if (result.getIsRight()==1)
                    {
                        sum = sum + obtainResultsDao.getScore(result.getPaperNumber(), result.getTopicId());
                    }
                }
                studentPaperListDto.setScore(sum);
            } else {
                studentPaperListDto.setIsSubmit(false);
                studentPaperListDto.setScore(0);
            }
            if (Objects.equals(testPaper.getIsPublish(), "是")) {
                    list.add(studentPaperListDto);
            }
        }
        return list;
    }
}
